home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / ISingleton.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-25  |  2.5 KB  |  89 lines

  1.  
  2. #ifndef __ISINGLETON_H__
  3. #define __ISINGLETON_H__
  4. /*
  5. Peon - Win32 Games Programming Library
  6. Copyright (C) 2002-2005 Erik Yuzwa
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this library; if not, write to the Free
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. Erik Yuzwa
  23. peon AT wazooinc DOT com
  24. */
  25.  
  26. /* Original version Copyright (C) Scott Bilas, 2000.
  27.  * All rights reserved worldwide.
  28.  *
  29.  * This software is provided "as is" without express or implied
  30.  * warranties. You may freely copy and compile this source into
  31.  * applications you distribute provided that the copyright text
  32.  * below is included in the resulting source code, for example:
  33.  * "Portions Copyright (C) Scott Bilas, 2000"
  34.  */
  35.  
  36. #include "Peonstdafx.h"
  37.  
  38. namespace peon 
  39. {
  40.  
  41.     /** 
  42.     * Template class for creating single-instance global classes.
  43.     * The code in this file is taken from article 1.3 in the the book:
  44.     * Game Programming Gems from Charles River Media with the 
  45.     * copyright notice going to Scott Bilas.
  46.     */
  47.     template <typename T> class PEONMAIN_API ISingleton
  48.     {
  49.     protected:
  50.  
  51.         /** The static member object */
  52.         static T* ms_Singleton;
  53.  
  54.     public:
  55.         /**
  56.         * Constructor
  57.         */
  58.         ISingleton( void )
  59.         {
  60.             assert( !ms_Singleton );
  61.             ms_Singleton = static_cast< T* >( this );
  62.         }
  63.  
  64.         /**
  65.         * Destructor
  66.         */
  67.         ~ISingleton( void )
  68.             {  assert( ms_Singleton );  ms_Singleton = 0;  }
  69.         
  70.         /**
  71.         * This method just returns the internal member by 
  72.         * reference
  73.         * @return T& - reference to internal abstract Type
  74.         */
  75.         static T& getSingleton( void )
  76.         {    assert( ms_Singleton );  return ( *ms_Singleton ); }
  77.         
  78.         /**
  79.         * This method just returns the internal member by 
  80.         * a pointer
  81.         * @return T* - pointer to the internal abstract Type
  82.         */
  83.         static T* getSingletonPtr( void )
  84.         { return ms_Singleton; }
  85.     };
  86. }
  87.  
  88. #endif
  89.